package com.sxhuayuan.parking.controller.admin;

import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.compenent.web.RespMessage.Status;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.entity.ParkingLot;
import com.sxhuayuan.parking.plugin.parking.ParkingPlugin;
import com.sxhuayuan.parking.service.ParkingLotService;
import com.sxhuayuan.parking.service.PluginService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/*停车列表*/
@RestController("adminParkingLotController")
@RequestMapping("/admin/parkinglot")
public class ParkingLotController extends BaseController {

	@Resource(name = "parkingLotServiceImpl")
	ParkingLotService parkingLotService;

	@Resource(name = "pluginServiceImpl")
	PluginService pluginService;

	/** 停车场分页条件查询 */
	@RequestMapping("/list")
	public RespMessage list(@RequestBody Pageable pageable, HttpServletRequest request) {
		return successResp(parkingLotService.findPage(pageable));
	}

	/** 根据ID查询 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public RespMessage get(Long id) {
		ParkingLot parkingLot = parkingLotService.findOne(id);
		return RespMessage.builder().success().setDatas(parkingLot).build();
	}

	/** 根据ID查询 */
	@RequestMapping(value = "/pluginlist", method = RequestMethod.GET)
	public RespMessage pluginList() {
		Map<String, String> map = pluginService.getParkingPlugins().stream().collect(Collectors.toMap(ParkingPlugin::getId, ParkingPlugin::getName));
		return RespMessage.builder().success().setDatas(map).build();
	}

	/** 保存 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public RespMessage save(@RequestBody @Validated ParkingLot parkingLot) {
		if (parkingLot.getWeixinRate() < parkingLot.getAgent().getWeixinRate()) {
			return RespMessage.error("微信费率不能小于代理本身的费率");
		}

		if (parkingLot.getAlipayRate() < parkingLot.getAgent().getAlipayRate()) {
			return RespMessage.error("支付宝费率不能小于代理本身的费率");
		}
		parkingLotService.save(parkingLot);
		return RespMessage.builder().success().setDatas(null).build();
	}

	/** 修改 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public RespMessage update(@RequestBody @Validated ParkingLot parkingLot) {
		if (parkingLot.getWeixinRate() < parkingLot.getAgent().getWeixinRate()) {
			return RespMessage.error("微信费率不能小于代理本身的费率");
		}

		if (parkingLot.getAlipayRate() < parkingLot.getAgent().getAlipayRate()) {
			return RespMessage.error("支付宝费率不能小于代理本身的费率");
		}
		parkingLotService.update(parkingLot);
		return RespMessage.builder().success().build();
	}

	/** 删除 */
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public RespMessage delete(Long id) {
		if (Objects.isNull(id) || id <= 0) {
			return RespMessage.error(Status.VALID_ERR, Message.FORM_VALID_FAIL);
		}
		ParkingLot parkingLot = parkingLotService.findOne(id);
		if (parkingLot == null) {
			return RespMessage.error(Status.ERROR, Message.PARKING_LOT_NOT_EXISTS);
		}
		parkingLotService.delete(parkingLot);
		return RespMessage.builder().success().build();
	}

	/** 获取所有车场 */
	@RequestMapping("/select")
	public RespMessage select() {
		return RespMessage.builder().success().setDatas(parkingLotService.findSelectOption(null)).build();
	}

}
